package com.googlecode.genie5day;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.widget.RemoteViews;

import java.util.Calendar;


public class Widget extends AppWidgetProvider {
    public static final String TAG = "Genie5Day";

    public static final String INTENT_NEW_FORECAST = "com.googlecode.genie5day.Widget.INTENT_NEW_FORECAST";
    public static final String EXTRA_FORECAST = "forecastExtra";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        context.startService(new Intent(context, UpdateService.class));

        Intent fiveDay = new Intent("android.intent.action.MAIN", null);
        fiveDay.setClassName(context, "com.googlecode.genie5day.FiveDayView");
        PendingIntent open5day = PendingIntent.getActivity(context, 0, fiveDay,
                PendingIntent.FLAG_CANCEL_CURRENT);
        try {
            for (int appWidgetId : appWidgetIds) {
                RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                        R.layout.widget);

                remoteViews.setTextViewText(R.id.weather_temp, Html.fromHtml("77&deg;"));
                remoteViews.setTextViewText(R.id.weather_high, Html.fromHtml("hi"));
                remoteViews.setTextViewText(R.id.weather_low, Html.fromHtml("59&deg;"));

                remoteViews.setOnClickPendingIntent(R.id.widget, open5day);

                appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
                Log.e(TAG, "Widget onUpdate() called: ");
            }
        } catch (Exception e) {
            Log.e(TAG, "Widget exception", e);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "Received intent: " + intent);

        if (intent.getAction().equals(INTENT_NEW_FORECAST)) {
            updateForecast(context, intent);
        }
        super.onReceive(context, intent);
    }

    private void updateForecast(Context context, Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        remoteViews.setTextViewText(R.id.weather_temp, Html.fromHtml(intent.getDoubleExtra("temp", 0) + "&deg;"));
        remoteViews.setTextViewText(R.id.weather_high, Html.fromHtml("" + intent.getDoubleExtra("windSpeed", 0)));
        remoteViews.setTextViewText(R.id.weather_low, Html.fromHtml("" + intent.getDoubleExtra("windDirection", 0)));

        ComponentName widget = new ComponentName(context, Widget.class);
        appWidgetManager.updateAppWidget(widget, remoteViews);
    }

}
